package com.definesys.smecrailtransit.module.RealStatus.ViewController;

/**
 * Created by cjy on 2017/8/23.
 */

import android.view.View;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.SmecManager.SmecRealm.RealmManager;
import com.definesys.smecrailtransit.adapter.SelctLineAdapter.SelectLineAdapter;
import com.definesys.smecrailtransit.adapter.SelectMonthAdapter.SelectMonthAdapter;
import com.definesys.smecrailtransit.adapter.SelectUserAdapter.SelectUserAdapter;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.bean.ApplyBean;
import com.definesys.smecrailtransit.bean.LineBean;
import com.definesys.smecrailtransit.bean.StationBean;
import com.definesys.smecrailtransit.databinding.FragmentRealStatusBinding;
import com.definesys.smecrailtransit.module.MonthForm.bean.PositionBean;
import com.definesys.smecrailtransit.module.RealStatus.RealStatusPresenter;
import com.definesys.smecrailtransit.utils.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;


import io.realm.RealmResults;


public class ViewController {
    private String selectUser;
    private String selectYear;
    private String selectLine;
    public String nowMonth;

    private ArrayList<StationBean> list;
    private ArrayList<PositionBean> popWindowList;
    private ArrayList<ApplyBean> userList;
    private ArrayList<String> lineList;
    private ArrayList<String> yearList;

    private FragmentRealStatusBinding fragmentRealStatusBinding;
    private RealStatusPresenter realStatusPresenter;
    protected RealmManager realmManager;

    private SelectMonthAdapter selectMonthAdapter;
    private SelectUserAdapter selectUserAdapter;
    private SelectLineAdapter selectLineAdapter;


    public ViewController(FragmentRealStatusBinding fragmentRealStatusBinding,RealStatusPresenter realStatusPresenter){
        this.fragmentRealStatusBinding = fragmentRealStatusBinding;
        this.realStatusPresenter = realStatusPresenter;
        this.realmManager=RealmManager.getInstance();

        this.list = new ArrayList<>();
        this.popWindowList = new ArrayList<>();
        this.userList = new ArrayList<>();
        this.lineList = new ArrayList<>();
        this.yearList = new ArrayList<>();

        this.selectLine = "12号线";
        this.selectUser = "全部运营商";


        
        //设置dialog背景层的点击事件
        fragmentRealStatusBinding.backgroundPop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setVisibility(View.GONE);
            }
        });

        fragmentRealStatusBinding.backgroundPull.setOnClickListener(v -> {
            fragmentRealStatusBinding.pull.setVisibility(View.GONE);
            fragmentRealStatusBinding.userImg.setBackgroundResource(R.drawable.month_form_bottom);

        });
        fragmentRealStatusBinding.line.setOnClickListener(v -> {
            fragmentRealStatusBinding.line.setVisibility(View.GONE);
            fragmentRealStatusBinding.lineImg.setBackgroundResource(R.drawable.month_form_bottom);
        });


        //设置线路下拉列表点击事件
        fragmentRealStatusBinding.choiceLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectLineAdapter.notifyDataSetChanged();
                if(fragmentRealStatusBinding.line.getVisibility() == View.GONE){
                    fragmentRealStatusBinding.line.setVisibility(View.VISIBLE);
                    fragmentRealStatusBinding.lineImg.setBackgroundResource(R.drawable.month_form_top);
                }else{
                    fragmentRealStatusBinding.line.setVisibility(View.GONE);
                    fragmentRealStatusBinding.lineImg.setBackgroundResource(R.drawable.month_form_bottom);
                }
                fragmentRealStatusBinding.pull.setVisibility(View.GONE);
                fragmentRealStatusBinding.userImg.setBackgroundResource(R.drawable.month_form_bottom);
                fragmentRealStatusBinding.backgroundPop.setVisibility(View.GONE);
            }
        });

        //设置运营商下拉列表点击事件
        fragmentRealStatusBinding.choiceUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectUserAdapter.select_user = selectUser;
                selectUserAdapter.notifyDataSetChanged();
                fragmentRealStatusBinding.invalidateAll();

                if(fragmentRealStatusBinding.pull.getVisibility() == View.GONE){
                    fragmentRealStatusBinding.pull.setVisibility(View.VISIBLE);
                    fragmentRealStatusBinding.userImg.setBackgroundResource(R.drawable.month_form_top);
                }else{
                    fragmentRealStatusBinding.pull.setVisibility(View.GONE);
                    fragmentRealStatusBinding.userImg.setBackgroundResource(R.drawable.month_form_bottom);
                }
                fragmentRealStatusBinding.line.setVisibility(View.GONE);
                fragmentRealStatusBinding.lineImg.setBackgroundResource(R.drawable.month_form_bottom);
                fragmentRealStatusBinding.backgroundPop.setVisibility(View.GONE);
            }
        });



    }

    public ArrayList<StationBean> getList() {
        return list;
    }

    public void setList(ArrayList<StationBean> list) {
        this.list = list;
    }

    public ArrayList<ApplyBean> getUserList() {
        return userList;
    }

    public void setUserList(ArrayList<ApplyBean> userList) {
        this.userList = userList;
    }

    public String getSelectUser() {
        return selectUser;
    }

    public void setSelectUser(String selectUser) {
        this.selectUser = selectUser;
    }

    public String getSelectLine() {
        return selectLine;
    }

    public void setSelectLine(String selectLine) {
        this.selectLine = selectLine;
    }

    public ArrayList<String> getLineList() {
        return lineList;
    }

    public void setLineList(ArrayList<String> lineList) {
        this.lineList = lineList;
    }

    public FragmentRealStatusBinding getfragmentRealStatusBinding() {
        return fragmentRealStatusBinding;
    }

    public void setfragmentRealStatusBinding(FragmentRealStatusBinding fragmentRealStatusBinding) {
        this.fragmentRealStatusBinding = fragmentRealStatusBinding;
    }

    public ArrayList<PositionBean> getPopWindowList() {
        return popWindowList;
    }

    public void setPopWindowList(ArrayList<PositionBean> popWindowList) {
        this.popWindowList = popWindowList;
    }

    public String getSelectYear() {
        return selectYear;
    }

    public void setSelectYear(String selectYear) {
        this.selectYear = selectYear;
    }

    public SelectMonthAdapter getSelectMonthAdapter() {
        return selectMonthAdapter;
    }

    public void setSelectMonthAdapter(SelectMonthAdapter selectMonthAdapter) {
        this.selectMonthAdapter = selectMonthAdapter;
    }

    public SelectUserAdapter getSelectUserAdapter() {
        return selectUserAdapter;
    }

    public void setSelectUserAdapter(SelectUserAdapter selectUserAdapter) {
        this.selectUserAdapter = selectUserAdapter;
    }

    public SelectLineAdapter getSelectLineAdapter() {
        return selectLineAdapter;
    }

    public void setSelectLineAdapter(SelectLineAdapter selectLineAdapter) {
        this.selectLineAdapter = selectLineAdapter;
    }

    //获取数据
    public void getNetWorkData(){
        //设置参数
        HashMap<String, String> param = new HashMap<>();

        param.put("line",this.getSelectLine().substring(0,this.getSelectLine().length()-2));

        //网络请求
        this.realStatusPresenter.getRealEleNum(param);

    }

    //获取本地数据
    public void getLocalData(){
        //获取线路数据
        RealmResults<LineBean> lineListRealm = (RealmResults<LineBean>)this.realmManager.findAllByKey(LineBean.class,"flag", SmecRailApi.MonthCode.REALSTATUS);
        ArrayList<LineBean> lineList = (ArrayList<LineBean>)this.realmManager.getRealm().copyFromRealm(lineListRealm);

        //获取运营商数据
        RealmResults<ApplyBean> applyList = (RealmResults<ApplyBean>)this.realmManager.findAllByKey(ApplyBean.class,"flag", SmecRailApi.MonthCode.REALSTATUS);
        this.userList = (ArrayList<ApplyBean>)this.realmManager.getRealm().copyFromRealm(applyList);
        ApplyBean allApply = new ApplyBean(-1,"全部运营商");
        this.userList.add(0,allApply);


        //获取站点信息
        int id=this.getNowLineId(lineList);
        if(this.selectUser.equals("全部运营商")){
            RealmResults<StationBean> stationList = (RealmResults<StationBean>)this.realmManager.findAllByKey(StationBean.class,"lineId",
                    id,"flag", SmecRailApi.MonthCode.REALSTATUS);
            this.list = (ArrayList<StationBean>)this.realmManager.getRealm().copyFromRealm(stationList);
        }else{
            RealmResults<StationBean> stationList = (RealmResults<StationBean>)this.realmManager.findAllByKey(StationBean.class,"lineId",
                    id,"applyId",this.getNowApplyId(),"flag", SmecRailApi.MonthCode.REALSTATUS);
            this.list = (ArrayList<StationBean>)this.realmManager.getRealm().copyFromRealm(stationList);
        }

        //排序处理
        ArrayList<StationBean> dataList = this.list;
        ArrayList<StationBean> resultList = new ArrayList<>();
        int length = this.list.size();
        for(int i = 0; i < length ;i++){

            int min = dataList.get(0).getSeq();
            int index =0;
            for(int j = 0; j < dataList.size() ;j++){
                if(dataList.get(j).getSeq() < min){
                    min = dataList.get(j).getSeq();
                    index = j;
                }
            }

            if(i != 0){
                if(resultList.get(i-1).getSeq() != dataList.get(index).getSeq()-1){
                    StationBean s = new StationBean();
                    s.setSeq(-1);
                    resultList.add(s);
                }
            }
            resultList.add(dataList.get(index));

            dataList.remove(index);
        }

        this.list = resultList;

    }

    private int getNowLineId(ArrayList<LineBean> lineList){

        if(CommonUtils.notEmpty(lineList)){
            for(int i=0 ;i<lineList.size();i++){
                if(lineList.get(i).getName().equals(this.selectLine)){
                    return lineList.get(i).getLineId();
                }
            }
            return lineList.get(0).getLineId();
        }
        return -1;
    }

    private int getNowApplyId(){
        for(int i=0 ;i<this.userList.size();i++){
            if(this.userList.get(i).getName().equals(this.selectUser)){
                return  this.userList.get(i).getApplyId();
            }
        }
        return this.userList.get(0).getApplyId();
    }

}


